/**
 * video info
 * Copyright (c) 2009 cmiscm.com
 * date of final modification : 2009. 11. 20 
*/
package com.cmiscm.videos.stream
{
	import com.cmiscm.utils.common.NumberUtilities;
	import com.cmiscm.videos.events.VideoTimeEvent;
	
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	public class CMinfo extends Sprite
	{	
		private var _realTime:Number = 0;	
		private var _totMin:int = 0;
		private var _totSec:int = 0;
		private var _min:int = 0;
		private var _sec:int = 0;
		
		private var _isDebug:Boolean;
		
		private var _refer:CMstream;
		
		public function CMinfo($stream:CMstream)
		{
			super();
			setting($stream);
		}
		private function setting($stream:CMstream):void
		{
			this.mouseChildren = false;
			this.mouseEnabled = false;
			
			_refer = $stream;
		}
				
		/**
		 * init 
		 */
		public function init():void
		{
			_realTime = 0;
			_totMin = 0;
			_totSec = 0;
			_min = 0;
			_sec = 0;
		}
		
		/**
		 * clear 
		 */		
		public function clear():void
		{
			_realTime = 0;
			_totMin = 0;
			_totSec = 0;
			_min = 0;
			_sec = 0;
		}
		
		/**
		 * destroy 
		 */		
		public function destroy():void
		{
			
		}
		
		/**
		 * update 
		 * 
		 */	
		public function update():void
		{
			_realTime = _refer.stream.time;
			
			// total play time
			_totMin = int(_refer.duration / 60);
			_totSec = int(_refer.duration % 60);
						
			// play time
			_min = int(_refer.stream.time / 60);
			_sec = int(_refer.stream.time % 60);
			
			// debug
			if (_isDebug) setDebug();
			
			this.dispatchEvent(new VideoTimeEvent(VideoTimeEvent.CHANGE_TIME, _realTime, _totMin, _totSec, _min, _sec));
		}
		
		/**
		 * debug 
		 * 
		 */		
		public function debugInfo():void
		{
			var t0:TextField = new TextField();
			setDebugTextFormat(t0, "t0", 50, 0);
			var t1:TextField = new TextField();
			setDebugTextFormat(t1, "t1", 100, 50);
			var t2:TextField = new TextField();
			setDebugTextFormat(t2, "t2", 90, 150);
			var t3:TextField = new TextField();
			setDebugTextFormat(t3, "t3", 100, 240);
			var t4:TextField = new TextField();
			setDebugTextFormat(t4, "t4", 80, 340);
			var t5:TextField = new TextField();
			setDebugTextFormat(t5, "t5", 130, 420);
			var t6:TextField = new TextField();
			setDebugTextFormat(t6, "t6", 110, 550);

			_isDebug = true;
		}
		
		/**
		 * debug mode play time 
		 */		
		private function setDebug():void
		{
			var t0:TextField = this.getChildByName("t0") as TextField;
			t0.text = "FPS : " + int(_refer.stream.currentFPS);
			var t1:TextField = this.getChildByName("t1") as TextField;
			t1.text = "time : " + NumberUtilities.addZeros(_min, 1) + ":" + NumberUtilities.addZeros(_sec, 1) + " / " + NumberUtilities.addZeros(_totMin, 1) + ":" + NumberUtilities.addZeros(_totSec, 1);
			var t2:TextField = this.getChildByName("t2") as TextField;
			t2.text = "real time : " + _refer.stream.time;
			var t3:TextField = this.getChildByName("t3") as TextField;
			t3.text = "buffer length : " + NumberUtilities.round(_refer.stream.bufferLength, 0.001);
			var t4:TextField = this.getChildByName("t4") as TextField;
			t4.text = "buffer time : " + _refer.stream.bufferTime;
			var t5:TextField = this.getChildByName("t5") as TextField;
			var bufferPercent:int = Math.min(Math.round(_refer.stream.bufferLength / _refer.stream.bufferTime * 100), 100);	
			t5.text = "buffer time percent : " + bufferPercent + "%";
			var t6:TextField = this.getChildByName("t6") as TextField;
			t6.text = "buffer percent : " + int(_refer.stream.bytesLoaded / _refer.stream.bytesTotal * 100) + "%";
		}
		
		/**
		 * textfield format 
		 */		
		private function setDebugTextFormat(txt:TextField, name:String, tw:int, tx:int):void
		{
			var format:TextFormat = new TextFormat();
            format.font = "_sans";
            format.color = 0xFFFFFF;
            format.size = 10;  
                      
            txt.background = true;
            txt.backgroundColor = 0x000000;
			txt.border = true;
			txt.borderColor = 0x666666;
			txt.name = name;
			txt.width = tw;
			txt.height = 17;
			txt.x = tx;
			txt.defaultTextFormat = format;	
					
			this.addChild(txt);
		}		
	}
}